import router from '@/router'
import store from '@/store'
// 引入进度条
import NProgress from 'nprogress'
// 引入进度条的样式
import 'nprogress/nprogress.css'
import getPageTitle from '@/utils/get-page-title'

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  NProgress.start()
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      console.log('您已经登录了 不能再回到登录页')
      next('/')
      NProgress.done()
    } else {
      console.log('已经登录了不是去登录页')
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      console.log('你已经是白名单放行')
      next()
    } else {
      console.log('您没有token 请重新登录')
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
